package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.DownLoadFileName;
import com.atguigu.constant.MessageConstant;
import com.atguigu.pojo.OrderSetting;
import com.atguigu.service.OrderSettingService;
import com.atguigu.utils.POIUtils;
import com.atguigu.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @author zhengkaiyuan
 * @version 1.0
 * @create 2021-06-22 19:22
 */
@RestController
@RequestMapping("/orderSetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;
    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile){
        try {
            // 使用poi工具类解析excel文件，读取里面的内容
            List<String[]> strings = POIUtils.readExcel(excelFile);
            // 把List<String[]> 数据转换成 List<OrderSetting>数据
            List<OrderSetting> orderSettings = new ArrayList<>();
            for (String[] string : strings) {
                orderSettings.add(new OrderSetting(new Date(string[0]),Integer.parseInt(string[1])));
            }
            orderSettingService.add(orderSettings);
            return new  Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return new  Result(true, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){
        try {
            List<Map<String,String>>  orderSettings= orderSettingService.getOrderSettingByMonth(date);

            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,orderSettings);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.GET_ORDERSETTING_FAIL);
        }

    }


    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try {
            System.out.println("orderSetting = " + orderSetting);
            orderSettingService.editNumberByDate(orderSetting);
            //预约设置成功
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //预约设置失败
            return new Result(true,MessageConstant.ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/downloadDate")
    public Result downloadDate(String date, HttpServletRequest request){
        String FilePath = request.getSession().getServletContext().getRealPath("/other/ordersetting");
        try {
            String name = orderSettingService.downloadDate(date, FilePath);
//            =================用于定时删除当天的文件=========================
            String fileNamePath =FilePath+File.separator+date + ".xlsx";
            jedisPool.getResource().sadd(DownLoadFileName.DATE_DOWN_LOAD_RESOURCES,fileNamePath);
//            ==============================================================
            return new Result(true,MessageConstant.DOWN_LOAD_DATE_SUCCESS,name);
        }catch (RuntimeException e){
            // 运行时异常，表示自由行和跟团游的关联表中存在数据
            return new Result(false,e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DOWN_LOAD_DATE_FAIL);
        }
    }

}
